package com.youlai.common.base;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.youlai.common.exception.BusinessException;
import com.youlai.core.mybatisplus.handler.WrappersFactory;
import com.youlai.core.security.model.SysUserDetails;
import com.youlai.system.entity.SysOrgEntity;
import com.youlai.system.util.SecurityUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * @param <M> 操作对象类型
 * @param <T> 操作对象类型
 * @author
 * @ClassName: BaseServiceImpl
 * @Description: service层基类
 * @date
 */
@Slf4j
//@Service
public class BaseServiceImpl<M extends LtBaseMapper<T,Long>, T extends MybatisBaseEntity> extends ServiceImpl<M, T> {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    protected M baseMapper;

    public BaseServiceImpl() {
    }

    public M getBaseMapper() {
        return this.baseMapper;
    }

    public Long queryPageCount(Map<String, Object> queryMap) {
        return baseMapper.queryPageCount(queryMap);
    }

    public List<T> queryPage(Map<String, Object> queryMap, RowBounds rowBounds) {
        List<T> list = this.baseMapper.queryPage(queryMap, rowBounds);
        return list;
    }

    public int insert(T record) {
        try {
            SysUserDetails user = SecurityUtils.getUser();
            record.setCreateBy(user.getUserId());
            record.setCreateName(user.getUsername());
            record.setUpdateBy(user.getUserId());
            record.setUpdateName(user.getUsername());
            return baseMapper.insert(record);
        } catch (Exception e) {
            throw new BusinessException(e);
        }
    }

    public int insertSelective(T record) {
        try {
            SysUserDetails user = SecurityUtils.getUser();
            record.setCreateBy(user.getUserId());
            record.setCreateName(user.getUsername());
            record.setUpdateBy(user.getUserId());
            record.setUpdateName(user.getUsername());
            return baseMapper.insert(record);
        } catch (Exception e) {
            throw new BusinessException(e);
        }
    }

    public int updateByPrimaryKey(T record) {
        try {
            SysUserDetails user = SecurityUtils.getUser();

            record.setUpdateBy(user.getUserId());
            record.setUpdateName(user.getUsername());
            return baseMapper.updateById(record);
        } catch (Exception e) {
            throw new BusinessException(e);
        }
    }

    public int updateByPrimaryKeySelective(T record, LambdaUpdateWrapper<T> updateWrapper) {
        try {
            SysUserDetails user = SecurityUtils.getUser();
            record.setUpdateBy(user.getUserId());
//            updateWrapper.set(record::getUpdateName, user.getUsername());
//            updateWrapper.set(record::getUpdateBy, user.getUserId());
            record.setUpdateName(user.getUsername());
            return baseMapper.update(record,updateWrapper);
        } catch (Exception e) {
            throw new BusinessException(e);
        }
    }

    public List<T> queryList(T record) {
        try {
            SysUserDetails user = SecurityUtils.getUser();
//            if (record.getOrgCode() == null && user!=null) {
//                record.setOrgCode(user.getOrgCode());
//            }
            return baseMapper.queryList(record);
        } catch (Exception e) {
            throw new BusinessException(e);
        }
    }

}
